﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PhamHoangDuy_KiemTra_1251tt1437.BLL;

namespace PhamHoangDuy_KiemTra_1251tt1437
{
    public partial class Form1 : Form
    {

        NHACCUNGCAPBLL BLL = new NHACCUNGCAPBLL();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            controls(true);
            controlTextbox(false);
            dataGridView1.DataSource = BLL.listNhaCungCap();
        }

        private void controls(bool edit) {
            btn_tim.Enabled = edit;
            btn_them.Enabled = edit;
            btn_luu.Enabled = !edit;
            btn_xoa.Enabled = edit;
        }

        private void controlTextbox(bool edit)
        {
            txt_tenncc.Enabled = edit;
            txt_diachi.Enabled = edit;
            txt_dienthoai.Enabled = edit;
            txt_email.Enabled = edit;
            txt_thanhpho.Enabled = edit;
        }

        private void ClearTextbox()
        {
            txt_mancc.Clear();
            txt_tenncc.Clear();
            txt_diachi.Clear();
            txt_dienthoai.Clear();
           
            txt_thanhpho.Clear();
            txt_email.Clear();
            txt_mancc.Focus();
        }



        private void btn_luu_Click(object sender, EventArgs e)
        {
            if (txt_mancc.Text.Equals("") || txt_tenncc.Text.Equals("") || txt_diachi.Text.Equals("") || txt_dienthoai.Text.Equals("") || txt_thanhpho.Text.Equals("") || txt_email.Text.Equals(""))
            {
                MessageBox.Show("Bạn Chưa Nhập Đầy Đủ Thông Tin.");
            }
            else {
                BLL.insert(txt_mancc.Text, txt_tenncc.Text, txt_diachi.Text, txt_dienthoai.Text, txt_thanhpho.Text,txt_email.Text);
                dataGridView1.DataSource = BLL.listNhaCungCap();
                controlTextbox(false);
                controls(true);
            }
        }

        private void btn_xoa_Click(object sender, EventArgs e)
        {
            BLL.delete(txt_mancc.Text);
            dataGridView1.DataSource = BLL.listNhaCungCap();
            ClearTextbox();
        }

        private void btn_them_Click(object sender, EventArgs e)
        {
            controls(false);
            controlTextbox(true);
            ClearTextbox();
            
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int row;
            row = e.RowIndex;

            txt_mancc.Text = dataGridView1[0, row].Value.ToString();
            txt_tenncc.Text = dataGridView1[1, row].Value.ToString();
            txt_diachi.Text = dataGridView1[2, row].Value.ToString();
            txt_dienthoai.Text = dataGridView1[3, row].Value.ToString();
            txt_thanhpho.Text = dataGridView1[4, row].Value.ToString();
            txt_email.Text = dataGridView1[5, row].Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           // reporting re = new reporting();
            //this.Close();
            //re.Show();
            
        }
    }
}
